package org.example.smartEducationBackend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.example.smartEducationBackend.entity.POJO.Resource;
import org.example.smartEducationBackend.entity.POJO.VideoRecs;
import org.example.smartEducationBackend.mapper.ResourceMapper;
import org.example.smartEducationBackend.mapper.UserRecsMapper;
import org.example.smartEducationBackend.mapper.VideoRecsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResourceService extends ServiceImpl<ResourceMapper, Resource>{
    @Autowired
    ResourceMapper resourceMapper;
    @Autowired
    UserRecsMapper userRecsMapper;
    @Autowired
    VideoRecsMapper videoRecsMapper;




    public Map<String,Object> getResources(Page<Resource> page,
                                        String type,
                                        Integer difficulty,
                                        List<String> labels,
                                        String search) {
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();

//         添加筛选条件
        if (type != null && !type.isEmpty()) {
            queryWrapper.eq("type", type);
        }
        if (difficulty != null) {
            queryWrapper.eq("difficulty", difficulty);
        }
        if (labels != null && !labels.isEmpty() ) {
            System.out.println(labels);
            for(String label: labels){
                queryWrapper.like("labels",label);

            }


//            queryWrapper.in("labels", String.join(",", labels));
        }
        if (search != null && !search.isEmpty()) {
            queryWrapper.and(wrapper -> wrapper.like("title", search).or().like("class_name", search));
        }
        // 构造返回结果
        // 执行分页查询
        IPage<Resource> resultPage = resourceMapper.selectPage(page, queryWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("total", resultPage.getTotal()); // 总条数
        result.put("records", resultPage.getRecords()); // 当前页的数据
        result.put("current", resultPage.getCurrent()); // 当前页码
        result.put("size", resultPage.getSize()); // 每页大小

        // 执行分页查询
        return result;
    }

    public List<Resource> getCFResource(Integer id) {
        List<Resource> resourceList = videoRecsMapper.selectBYId(id);


        return resourceList;
    }
}